package com.jader.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.jader.adapter.SpinnerAdapter;
import com.jader.myviews.MyProgressDialog;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class AddressInsertActivity extends BaseActivity {
	ImageView insert_back;
	TextView insert_title;
	EditText edit_insert_receiver, edit_insert_phone, edit_insert_detail;
	Button btn_insert_finish;
	private String url = JaderApplication.getIP() + "/Jader/client";
	private HttpUtils httpUtils;
	private String receiver, phone, detail;
	private Spinner privoice, city, area;
	private SpinnerAdapter privoiceAdapter, cityAdapter, areaAdapter;
	private String privoiceString, cityString, areaString;
	private MyProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address);
		initView();
		initProvinceDatas();
		viewListener();
	}

	private void viewListener() {
		insert_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		btn_insert_finish.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				checkEdit();
			}
		});
	}

	/** 插入地址 */
	protected void insertAddress() {
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "" + JaderApplication.ADDRESS_INSERT);
		params.addBodyParameter("receiver", receiver);
		params.addBodyParameter("phoneNum", phone);
		params.addBodyParameter("detail", detail);
		params.addBodyParameter("province", privoiceString);
		params.addBodyParameter("city", cityString);
		params.addBodyParameter("area", areaString);
		if (JaderApplication.user == null) {
			JaderApplication.login(this);
		} else {
			params.addBodyParameter("userid",
					"" + JaderApplication.user.getUserid());
			httpUtils = new HttpUtils();
			dialog = JaderApplication.getMyDialog(this);
			dialog.show();
			httpUtils.send(HttpMethod.POST, url, params,
					new RequestCallBack<String>() {

						@Override
						public void onFailure(HttpException arg0, String arg1) {
							JaderApplication.show(getBaseContext(), "插入失败");
							if (dialog.isShowing()) {
								dialog.dismiss();
							}
						}

						@Override
						public void onSuccess(ResponseInfo<String> info) {
							String result = info.result;
							if (result.equals("success")) {
								JaderApplication.show(getBaseContext(), "插入成功");
								startActivity(new Intent(getBaseContext(),
										AddressManagerActivity.class));
							} else {
								JaderApplication.show(getBaseContext(), "再试试吧");
							}
							if (dialog.isShowing()) {
								dialog.dismiss();
							}
						}
					});
		}
	}

	private void initView() {
		insert_back = (ImageView) findViewById(R.id.top_actionbar_back);
		insert_title = (TextView) findViewById(R.id.top_actiobar_text);
		insert_title.setText("新增收货地址");
		edit_insert_receiver = (EditText) findViewById(R.id.edit_address_receiver_name);
		edit_insert_phone = (EditText) findViewById(R.id.edit_address_receiver_phone);
		edit_insert_detail = (EditText) findViewById(R.id.edit_address_receiver_detail);
		btn_insert_finish = (Button) findViewById(R.id.btn_address_finish);
		privoice = (Spinner) findViewById(R.id.province);
		city = (Spinner) findViewById(R.id.city);
		area = (Spinner) findViewById(R.id.area);
	}

	@Override
	protected void onResume() {
		initData();
		super.onResume();
	}

	private void initData() {
		privoiceAdapter = new SpinnerAdapter(this, mProvinceDatas);
		privoice.setAdapter(privoiceAdapter);
		privoice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// position为当前省级选中的值的序号
				privoiceString = mProvinceDatas[arg2];
				// 将地级适配器的值改变为city[position]中的值
				cityAdapter = new SpinnerAdapter(AddressInsertActivity.this,
						mCitisDatasMap.get(privoiceString));
				// 设置二级下拉列表的选项内容适配器
				city.setAdapter(cityAdapter);

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}

		});
		city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				cityString = mCitisDatasMap.get(privoiceString)[arg2];
				areaAdapter = new SpinnerAdapter(AddressInsertActivity.this,
						mDistrictDatasMap.get(cityString));
				area.setAdapter(areaAdapter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		area.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				areaString = mDistrictDatasMap.get(cityString)[arg2];

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 检查edit是否为空
	 */
	private void checkEdit() {
		receiver = edit_insert_receiver.getText().toString().trim();
		phone = edit_insert_phone.getText().toString().trim();
		detail = edit_insert_detail.getText().toString().trim();
		if (receiver.equals("")) {
			JaderApplication.show(this, "请输入收货人");
		} else if (phone.equals("")) {
			JaderApplication.show(this, "请输入联系电话");
		} else if (detail.equals("")) {
			JaderApplication.show(this, "请输入详细地址");
		} else {
			insertAddress();
		}
	}
}
